package com.siyue.mq.config;

import com.siyue.mq.constants.RabbitMqConstant;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.logging.Logger;

@Configuration

public class RabbitMqConfig {

    @Bean
    public Queue queue(){
        return new Queue(RabbitMqConstant.MSG_QUEUE_NAME);
    }

    @Bean
    public TopicExchange directExchange(){
        return new TopicExchange(RabbitMqConstant.MSG_EXCHANGE_NAME);
    }
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(directExchange()).with(RabbitMqConstant.MSG_ROUTING_KRY_NAME);

    }



    /**
     * 创建死信交换机
     * @return
     */
    @Bean
    public TopicExchange deadDirectExchange(){
        return new TopicExchange(RabbitMqConstant.MSG_EXCHANGE_NAME_DEAD);
    }
    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadQueue() {
        return new Queue(RabbitMqConstant.MSG_QUEUE_NAME_DEAD);
    }

    /**
     * 死信队列绑定
     * @return
     */
    @Bean
    public Binding bindDeadBuildersRouteKey() {
        return BindingBuilder.bind(deadQueue()).to(deadDirectExchange()).with(RabbitMqConstant.MSG_ROUTING_KRY_NAME);
    }


    /**
     * 基于消息事务的处理方式，当消费失败进行重试，有时间间隔，当达到超时时间，就发到死信队列，等待人工处理.
     * @return
     */
/*
    @Bean
    public Queue testQueue() {
        //设置死信交换机
        return QueueBuilder.durable(QUEUE).withArgument("x-dead-letter-exchange", RabbitMqConstant.MSG_EXCHANGE_NAME_DEAD)
                //毫秒
                .withArgument("x-message-ttl", 2000)
                //设置死信routingKey
                .withArgument("x-dead-letter-routing-key", RabbitMqConstant.MSG_ROUTING_KRY_NAME).build();
    }
*/


}
